// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const missionMap = new Map();
const db = cloud.database();
const ONE_DAY = 1000 * 60 * 60 * 24
// 云函数入口函数
exports.main = async (event, context) => new Promise((resolve, reject) => {
    const wxContext = cloud.getWXContext()
    missionMap.clear();
    console.log(event)
    const { id } = event;
    getMissionItem(id).then(item => {
        if (!item.init)
            resolve(item)
        else if (item.start && item.end) {
            resolve(item)
        } else {
            figureOutStartDate(item).then(res => {
                if (res) {
                    figureOutEndDate(item).then(()=>{
                        resolve(item)
                    })
                } else {
                    resolve(item)
                }
            })
        }
    }).catch(err => reject(err))
});
function getMissionItem(id) {
    if (missionMap.has(id)) return missionMap.get(id);
    return db.collection('mission').doc(id).get().then(res => {
        if (!res.data.init) {
            return db.collection('user').doc(res.data.consignor).get().then(usr => {
                res.data.consignorClient = usr.data;
                return res.data
            })
        } else {
            missionMap.set(id, res.data)
            return res.data
        }
    })
}
async function figureOutEndDate(item) {
    if (item.end) return item.end;
    else {
        let end = null;
        if (item.front && item.front.length) {
            let startPoint = Date.parse(await figureOutStartDate(item));
            if (startPoint) {
                for (let j = 0; j < item.last; startPoint += ONE_DAY) {
                    const tempDate = new Date(startPoint);
                    const day = tempDate.getDay();
                    //排除周六周日
                    if (day !== 0 && day !== 6) {
                        j++;
                    }
                }
                startPoint -= ONE_DAY;
                item.end = end = new Date(startPoint).toLocaleDateString();
            }
        }
        // console.log('任务起始时间', start);
        return end;
    }
}
async function figureOutStartDate(item) {
    if (item.start) return item.start;
    else {
        let start = null;
        if (item.front && item.front.length) {
            for (const id of item.front) {
                const targetMission = await getMissionItem(id);
                const targetDateObject = await figureOutEndDate(targetMission);

                let targetDate;
                if (targetDateObject) {
                    targetDate = Date.parse(targetDateObject) + ONE_DAY;
                    if (!start || start < targetDate) {
                        start = targetDate;
                    }
                }

            }
            if (start) item.start = start = new Date(start).toLocaleDateString();
        }
        // console.log('任务起始时间', start);
        return start;
    }
}